package com.ruoyi.web.controller.transplant;

import java.util.ArrayList;
import java.util.List;

import com.ruoyi.common.core.domain.entity.SysRole;
import com.ruoyi.common.core.domain.entity.SysUser;
import com.ruoyi.common.utils.ServletUtils;
import com.ruoyi.framework.web.service.TokenService;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.transplant.domain.Transplant;
import com.ruoyi.transplant.service.ITransplantService;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 出圃管理Controller
 *
 * @author ruoyi
 * @date 2021-07-12
 */
@RestController
@RequestMapping("/transplant/transplant")
public class TransplantController extends BaseController
{
    @Autowired
    private ITransplantService transplantService;
    @Autowired
    private TokenService tokenService;

    /**
     * 查询出圃管理列表
     */
    @PreAuthorize("@ss.hasPermi('transplant:transplant:list')")
    @GetMapping("/list")
    public TableDataInfo list(Transplant transplant)
    {
        startPage();
        List<Transplant> list=null;
        SysUser user = tokenService.getLoginUser(ServletUtils.getRequest()).getUser();
        List<SysRole> roles = user.getRoles();
        for (SysRole role : roles) {

            if ("超级管理员".equals(role.getRoleName()) || "总经理".equals(role.getRoleName()) || "财务采购经理".equals(role.getRoleName()) || "销售经理".equals(role.getRoleName())) {
                list = transplantService.selectTransplantList(transplant);
            }

//            else if ( "片区主管".equals(role.getRoleName()) || "棚长".equals(role.getRoleName()) || "棚员".equals(role.getRoleName())) {
//                //这三个角色只能看自己的，且看不到价格，在sql去掉价格字段
//                list = transplantService.selectTransplantById(user.getUserId());
//            }

            else
                list = transplantService.selectTransplantListWithoutPrices(transplant);
        }
        return getDataTable(list);
    }

    /**
     * 导出出圃管理列表
     */
    @PreAuthorize("@ss.hasPermi('transplant:transplant:export')")
    @Log(title = "出圃管理", businessType = BusinessType.EXPORT)
    @GetMapping("/export")
    public AjaxResult export(Transplant transplant)
    {
        List<Transplant> list=new ArrayList();
        //通过id来取
        Long a[]=transplant.getTransplantIds();
        for(int i=0;i<a.length;i++) {
            Transplant list1 = transplantService.selectTransplantById(a[i]);
            list.add(list1);
        }
        ExcelUtil<Transplant> util = new ExcelUtil<Transplant>(Transplant.class);
        return util.exportExcel(list, "出圃管理数据");
    }

    /**
     * 获取出圃管理详细信息
     */
    @PreAuthorize("@ss.hasPermi('transplant:transplant:query')")
    @GetMapping(value = "/{transplantId}")
    public AjaxResult getInfo(@PathVariable("transplantId") Long transplantId)
    {
        return AjaxResult.success(transplantService.selectTransplantById(transplantId));
    }

    /**
     * 新增出圃管理
     */
    @PreAuthorize("@ss.hasPermi('transplant:transplant:add')")
    @Log(title = "出圃管理", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody Transplant transplant)
    {
        return toAjax(transplantService.insertTransplant(transplant));
    }

    /**
     * 修改出圃管理
     */
    @PreAuthorize("@ss.hasAnyPermi('transplant:transplant:edit,transplant:transplant:permit1,transplant:transplant:permit2')")
    @Log(title = "出圃管理", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody Transplant transplant)
    {
        return toAjax(transplantService.updateTransplant(transplant));
    }

    /**
     * 删除出圃管理
     */
    @PreAuthorize("@ss.hasPermi('transplant:transplant:remove')")
    @Log(title = "出圃管理", businessType = BusinessType.DELETE)
	@DeleteMapping("/{transplantIds}")
    public AjaxResult remove(@PathVariable Long[] transplantIds)
    {
        return toAjax(transplantService.deleteTransplantByIds(transplantIds));
    }
}
